package com.petsdar.controller;

import com.petsdar.pojo.Shop;
import com.petsdar.service.ShopService;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/3
 * Time: 10:39
 * Description: 店铺相关接口处理
 * Version: V1.0
 */

@RestController
public class ShopController {
    /** shop相关业务逻辑处理实例对象 */
    private ShopService shopService;

    @Autowired
    public void setShopService(ShopService shopService) {
        this.shopService = shopService;
    }

    /**
     * 获取所有店铺信息
     * @return
     */
    @GetMapping("/chain")
    public String getShop(){
        List<Shop> shops = shopService.getShop();
        if (!shops.isEmpty()){
            return ResultUtil.success(shops);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加新的店铺信息
     * @param shopJson 店铺信息相关json数据
     * @return
     */
    @PostMapping("/chain")
    public String addShop(@RequestBody String shopJson){
        boolean result = shopService.addShop(shopJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 更新店铺信息
     * @param shopJson 店铺信息相关json数据
     * @return
     */
    @PutMapping("/chain")
    public String updateShop(@RequestBody String shopJson){
        boolean result = shopService.updateShop(shopJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 通过店铺id删除店铺信息
     * @param shopId 店铺id
     * @return
     */
    @DeleteMapping("/chain")
    public String delShop(@RequestParam("shopId") String shopId){
        boolean result = shopService.delShop(shopId);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 获取所有店铺名称
     * @return
     */
    @GetMapping("/shopinfo")
    public String getShopInfo(){
        List<Shop> shopInfo = shopService.getShopInfo();
        if (!shopInfo.isEmpty()){
            return ResultUtil.success(shopInfo);
        }
        return ResultUtil.fail();
    }
}
